{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from utils import *\n",
    "from constant import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def getFilter(filter_key):\n",
    "    filter_names = filter_key.split(\"-\")[:-1]\n",
    "    the_filter = combineFilters(filter_names)\n",
    "    if with_recent:\n",
    "        recent_filter = recent(int(filter_key[-1]),data_df)\n",
    "    return the_filter & recent_filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def is_necessary(cross_df):\n",
    "    if len(cross_df) == 0:\n",
    "        return False\n",
    "    cross_df = cross_df.sort_values(\"minute\")\n",
    "    cross_df = cross_df.groupby(\"date\").first()\n",
    "    win_df = cross_df[cross_df[\"change\"] > 1.0]\n",
    "    win_ratio = float(len(win_df))/len(cross_df)\n",
    "#     print cross_df.change.mean(),win_ratio,len(cross_df)\n",
    "    if len(cross_df) <= 3 and win_ratio > 0.5:\n",
    "        return False\n",
    "    if cross_df.change.mean() < 1.02:\n",
    "        return True\n",
    "    if win_ratio < 0.6:\n",
    "        return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def reduceFilers(filter_key):\n",
    "    filter_names = filter_key.split(\"-\")[:-1]\n",
    "    recent_filter = recent(int(filter_key[-1]),data_df)\n",
    "    the_filter = combineFilters(filter_names)\n",
    "    valid_names = []\n",
    "#     print len(data_df[the_filter & recent_filter])\n",
    "    for filter_name in filter_names:\n",
    "        sub_filter_names = [name for name in filter_names if name != filter_name]\n",
    "        sub_filter = combineFilters(sub_filter_names)\n",
    "        cross_df = data_df[sub_filter & ~the_filter & recent_filter & ~yz2]\n",
    "#         print filter_name\n",
    "        if is_necessary(cross_df):\n",
    "            valid_names.append(filter_name)\n",
    "    return \"-\".join(valid_names) + \"-\" + filter_key[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/site-packages/ipykernel/__main__.py:10: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['~opentop2-speedup1-~speedup2-~jump2-foot0-~small_capq-minute-2', '~jump1-jump2-foot1-foot2-~isnew-~small_capq-small_volume-~opentop1-2', '~speedup1-~jump1-speedup2-foot0-foot1-~small_capq-2', '~opentop2-~jump2-~foot0-~foot2-isnew-small_volume-2', 'speedup1-~jump1-speedup2-~foot1-~foot2-~isnew-small_capq-small_volume-2', '~opentop2-~speedup1-~speedup2-foot1-isnew-minute-2', '~opentop2-speedup1-foot1-foot2-small_volume-2', '~opentop2-~speedup1-jump1-jump2-~foot1-small_volume-2']\n",
      "['~foot1-small_volume-yz0-3', '~opentop2-~jump1-~jump2-foot0-~isnew-small_capq-minute-3', 'jump1-~foot1-small_volume-yz0-3', '~speedup1-jump1-~speedup2-~jump2-~foot1-~foot2-minute-~small_volume-~opentop0-~opentop1-3', '~opentop2-~speedup1-jump2-foot0-~foot1-small_volume-~opentop1-3', 'jump2-foot0-~foot1-foot2-small_capq-small_volume-3', '~opentop2-~speedup2-jump2-foot0-~foot1-~foot2-small_capq-~small_volume-3', '~opentop2-jump1-jump2-~foot1-foot2-~small_capq-minute-~opentop1-3', '~opentop2-~jump1-~jump2-~foot0-~small_capq-minute-3', 'opentop2-~speedup1-jump1-~foot1-~foot2-~opentop1-3', '~opentop2-speedup1-foot0-~foot2-~small_capq-small_volume-3', 'jump1-speedup2-foot1-~isnew-small_capq-small_volume-~opentop1-3', 'opentop2-jump2-foot1-minute-~opentop1-3', '~opentop2-jump2-foot0-~foot1-~foot2-~small_capq-small_volume-3', '~speedup1-speedup2-foot0-~foot1-~isnew-small_capq-small_volume-~opentop1-3', '~speedup1-~jump2-foot0-~foot1-minute-opentop1-3', '~opentop2-speedup1-jump1-jump2-~foot1-foot2-~small_capq-~opentop1-3', '~opentop2-~speedup1-foot0-~foot1-minute-~small_volume-opentop1-3']\n",
      "['~opentop2-~speedup1-~foot2-~isnew-~small_capq-minute-small_volume-~opentop1-4', '~speedup1-~jump2-~foot1-small_capq-small_volume-top0-4', '~opentop2-jump1-minute-small_volume-opentop1-4', '~speedup1-jump1-~speedup2-~foot0-~foot2-~isnew-minute-~opentop0-4', '~speedup1-jump1-jump2-~foot1-foot2-small_volume-4', '~opentop2-~speedup1-jump1-~speedup2-~foot1-~foot2-~small_capq-minute-4', '~opentop2-small_volume-yz0-4', '~opentop2-~speedup1-jump1-~speedup2-~foot1-minute-small_volume-4', '~opentop2-~speedup1-jump1-foot0-~foot1-minute-small_volume-4', '~opentop2-~jump1-speedup2-small_capq-small_volume-4', '~speedup1-foot0-~foot1-foot2-small_volume-4', '~speedup1-~jump2-~foot1-small_capq-minute-small_volume-4', '~foot1-small_capq-minute-small_volume-yz0-4', '~opentop2-~speedup2-~foot0-small_capq-minute-small_volume-~opentop0-top0-4', '~opentop2-jump1-minute-small_volume-~yz1-opentop1-4', '~opentop2-jump1-minute-small_volume-~yz1-4', '~opentop2-jump2-foot2-small_capq-small_volume-4', '~opentop2-foot2-small_capq-minute-4']\n",
      "['jump1-~speedup2-~foot2-~isnew-minute-~yz1-opentop1-5', '~speedup1-~speedup2-~foot0-minute-~small_volume-~opentop1-5', '~jump1-~speedup2-~foot1-~isnew-~opentop1-5', 'speedup1-~speedup2-~foot0-~foot2-small_volume-~opentop0-5', '~opentop2-~speedup1-~jump1-~foot1-5']\n",
      "['~opentop2-jump1-small_capq-minute-small_volume-6', '~opentop2-foot0-small_capq-minute-6', '~speedup2-~foot2-~isnew-small_capq-minute-small_volume-6']\n",
      "['~foot2-small_volume-yz0-7']\n"
     ]
    }
   ],
   "source": [
    "for filters in filterss:\n",
    "    each_filters = []\n",
    "    for filter_key in filters:\n",
    "        reduced_filter = reduceFilers(filter_key)\n",
    "        each_filters.append(reduced_filter)\n",
    "    print each_filters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
